package com.baizhi.controller;

import com.baizhi.entity.StudentTag;
import com.baizhi.service.TagsStudentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("tagsstudent")
public class TagsStudentController {

    private static final Logger log = LoggerFactory.getLogger(TagsStudentController.class);
    @Autowired
    private TagsStudentService tagsStudentService;

    /**
     * 查询所有学生id对应的标签id
     *
     * @param sid
     * @return
     */
    @HystrixCommand(fallbackMethod = "HystrixQueryAll")
    @GetMapping
    public ArrayList<Long> queryAll(@RequestParam("id") Long sid) {
        List<StudentTag> studentTags = tagsStudentService.queryAll(sid);
        ArrayList<Long> longs = new ArrayList<>();
        for (StudentTag studentTag : studentTags) {
            Long tid = studentTag.getTid();
            longs.add(tid);
        }
        return longs;
    }

    public ArrayList<Long> HystrixQueryAll(@RequestParam("id") Long sid) {
        ArrayList<Long> longs = new ArrayList<>();
        Long i = -1L;
        longs.add(i);
        return longs;
    }


    /**
     * 添加标签
     *
     * @param studentTag
     */
    @HystrixCommand(fallbackMethod = "HystrixAddTags")
    @PostMapping
    public void addTags(@RequestBody StudentTag studentTag) {
        log.info("studentTag {}", studentTag);
        tagsStudentService.add(studentTag);
    }

    public void HystrixAddTags(@RequestBody StudentTag studentTag) {
        log.info("studentTag {}", studentTag);
        return;
    }
}
